import { observable, action } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import { ComsStore } from './tables/common';
import * as API_DOC_RECYCLE from '../apis/DocRecycle';
import { message } from 'antd';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class DocRecycleStore extends ComsStore {
  @observable title = getLabel(130650, '文档回收站');
  @observable selectedTreeKeys = [];
  @observable expandedKey = [];
  @observable tabData = [];
  @observable showSearchAd = false;
  @observable visible = false;
  @observable isOpen = true;
  @observable module = 'DocRecycle';
  @action

  setTitle = (title) => {
    this.title = title;
  }

  setSelectedTreeKeys = (key, keyid) => {
    this.selectedTreeKeys = key;
    this.doSearch(this.module, key.length > 0 ? { seccategory: keyid } : {});
    this.comsWeaTableStore.setSelectedRowKeys([]);
    if (key.length == 0) {
    	this.setTabData([]);
    	this.setExpandedKey([]);
    } else {
	    this.treeDatas.forEach((item) => {
        const tempResult = this.getItem(item, key);
	        tempResult.forEach((iteml) => {
	          if (iteml.getNode) {
	            const temptreeData = this.getShowItem(tempResult, iteml.item.domid, iteml.item.pid);
	            this.setTabData(temptreeData);
	            let tempExpandedkey = [];
	            temptreeData.forEach((i) => {
	              tempExpandedkey.push(i.domid);
	            });
	            this.setExpandedKey(tempExpandedkey);
	          }
	        });
	    });
    }
  }

  setShowSearchAd = (showSearchAd) => {
    this.showSearchAd = showSearchAd;
  }
  setVisible = (visible) => {
    this.visible = visible;
  }
  setIsOpen = (isOpen) => {
  	this.isOpen = isOpen;
  }
  setTabData = (tabData) => {
    this.tabData = tabData;
  }
  setExpandedKey = (expandedKey) => {
    this.expandedKey = expandedKey;
  }


  resumeOfRecycle = (params, searchParams = {}) => {
     API_DOC_RECYCLE.operateOfRecycle(params).then((data) => {
      this.visible = false;
      if (data.status === 1) {
      	message.success(data.msg);
        this.doSearch('DocRecycle', searchParams);
        this.getTreeDatas(this.module, params);
      } else {
      	message.error(data.msg);
      }
    });
  }
  deleteOfRecycle = (params, searchParams = {}) => {
    API_DOC_RECYCLE.operateOfRecycle(params).then((data) => {
      this.visible = false;
      if (data.status === 1) {
      	message.success(data.msg);
        this.doSearch('DocRecycle', searchParams);
        this.getTreeDatas(this.module);
      } else {
      	message.error(data.msg);
      }
    });
  }

  isOpenRecycle = () => {
  	this.selectedTreeKeys = [];
  	this.setTabData([]);
    this.setExpandedKey([]);
    this.form.reset();
  	API_DOC_RECYCLE.isOpenCycle().then((data) => {
      if (data.status === 1) {
        this.isOpen = true;
        this.getConditionInfo(this.module);
	    this.doSearch(this.module);
	    this.getTreeDatas(this.module);
	    this.getRightMenu(this.module);
      } else {
      	this.isOpen = false;
      }
    });
  }

  getItem = (item, selected) => {
    let getNode = false;
      let returnItem = [];
      if (item.domid === selected[0]) {
        getNode = true;
        returnItem.push({ item, getNode });
      } else {
        returnItem.push({ item, getNode });
        if (item.haschild) {
          item.childs.forEach((i) => {
            let tempResult = [];
            tempResult = this.getItem(i, selected);
            returnItem = returnItem.concat(tempResult);
          });
        }
      }
    return returnItem;
  }
  getShowItem=(tempResult, domid, pid) => {
    let result = [];
    let temp = [];
    tempResult.forEach((item) => {
      if (item.item.domid === domid) {
        result.unshift(item.item);
      }
    });
    tempResult.forEach((item) => {
      if (item.item.key === pid) {
        result.unshift(item.item);
        temp = [];
        if (pid !== '') {
          temp = this.getShowItem(tempResult, item.item.domid, item.item.pid);
          temp.pop();
          result = temp.concat(result);
        }
      }
    });
    return result;
  }
}

